class SessionController < ApplicationController

   require 'socket'

   def gotogoogle

    redirect_to "https://www.google.com/accounts/o8/ud
?openid.ns=http://specs.openid.net/auth/2.0
&openid.ns.pape=http://specs.openid.net/extensions/pape/1.0
&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select
&openid.identity=http://specs.openid.net/auth/2.0/identifier_select
&openid.return_to=http://"+request.host_with_port+"/home
&openid.realm=http://"+request.host_with_port+"/
&openid.assoc_handle=ABSmpf6DNMw
&openid.mode=checkid_setup
&openid.ui.ns=http://specs.openid.net/extensions/ui/1.0
&openid.ui.mode=popup
&openid.ui.icon=true
&openid.ns.ax=http://openid.net/srv/ax/1.0
&openid.ax.mode=fetch_request
&openid.ax.type.email=http://axschema.org/contact/email
&openid.ax.type.language=http://axschema.org/pref/language
&openid.ax.type.firstname=http://axschema.org/namePerson/first
&openid.ax.type.lastname=http://axschema.org/namePerson/last
&openid.ax.required=email,language,firstname,lastname"

  end

  #Metodo que redirecciona al API de Google OpenID cuando se trata del
  #login de un usuario existente. Si la autenticacion con Google es exitosa,
  #redirecciona al home y le da acceso al usuario a las funcionalidades de
  #usuario
  def clientgotogoogle

    redirect_to "https://www.google.com/accounts/o8/ud
?openid.ns=http://specs.openid.net/auth/2.0
&openid.ns.pape=http://specs.openid.net/extensions/pape/1.0
&openid.claimed_id=http://specs.openid.net/auth/2.0/identifier_select
&openid.identity=http://specs.openid.net/auth/2.0/identifier_select
&openid.return_to=http://"+request.host_with_port+"/session/client_login
&openid.realm=http://"+request.host_with_port+"/
&openid.assoc_handle=ABSmpf6DNMw
&openid.mode=checkid_setup
&openid.ui.ns=http://specs.openid.net/extensions/ui/1.0
&openid.ui.mode=popup
&openid.ui.icon=true
&openid.ns.ax=http://openid.net/srv/ax/1.0
&openid.ax.mode=fetch_request
&openid.ax.type.email=http://axschema.org/contact/email
&openid.ax.type.language=http://axschema.org/pref/language
&openid.ax.type.firstname=http://axschema.org/namePerson/first
&openid.ax.type.lastname=http://axschema.org/namePerson/last
&openid.ax.required=email,language,firstname,lastname"

  end

  def client_login

    request.query_string.split(/&/).inject({}) do |hash, setting|
      key, val = setting.split(/=/)
      if key == 'openid.mode'
        @cancel = val
      end
      if key == 'openid.ext1.value.email'
        usuario, correo  = val.split(/%40/)
        @email = usuario + '@' + correo
      end
    end

    if @cancel=='cancel'
      redirect_to :controller => 'home', :action => 'index'
    else
      @client= Cliente.find(:first, :conditions => [" correo = ?", @email ])
      if @client
          session[:user] = @client.usuario
          session[:correo]= @client.correo
          session[:type] = @client.root
          session[:id] = @client.id
          redirect_to :controller => 'clientes', :action => 'show', :id => @client.id
      else
        redirect_to :controller => 'home', :action => 'index'
      end
    end

  end


end
